package FirstChapter.five;

import java.util.Scanner;

/**
 * Created by Lee on 2017/2/18.
 */
public class UnionQuickFindWithWeight {
    private int[] id;//Id of component
    private int count;//count of component
    private int sz[];

    /*
    * init Compoint  Array*/
    public UnionQuickFindWithWeight(int N) {
        count = N;
        id = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
        }
        sz = new int[N];
        for (int i=0;i<N;i++) {
            sz[i] = 1;
        }
    }

    public int count (){
        return count;
    }

    public boolean isConnected(int p, int q) {
        return find(p) == find(q);
    }

    public int find(int p) {
        while (p != id[p]) {
            p = id[p];
        }
        return p;
    }

    public void union(int p, int q) {
        int pRoot = find(p);
        int qRoot = find(q);

        if (pRoot == qRoot) {
            return;
        }
        if (sz[pRoot] < sz[qRoot]) {
            id[pRoot] = id[qRoot];
            sz[qRoot] += sz[pRoot];
        } else {
            id[qRoot] = id[pRoot];
            sz[pRoot] += sz[qRoot];
        }
        count--;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = Integer.parseInt(sc.next());
        UnionQuickFindWithWeight uf = new UnionQuickFindWithWeight(N);
        while (true) {
            int p = Integer.parseInt(sc.next());
            int q = Integer.parseInt(sc.next());
            if (p == -1 || q == -1) {
                break;
            }
            uf.union(p, q);
        }
        System.out.println(uf.count() + "components");
    }
}
